﻿//BestSeller.aspx.cs
////////////////////////////////////////
////////////////////////////////////////
//当点击首页的热门商品时显示此页，即显示所有的热门商品
////////////////////////////////////////
////////////////////////////////////////
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

public partial class Bestseller : System.Web.UI.Page
{
    CommonClass ccObj = new CommonClass();
    ArrayList orderList = new ArrayList();  //存放订单列表，存放的数据类型为order类。
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //页面首次加载时,将当前页码设置为"1",并保存到Session中
            this.lbPage.Text = "1";
            Session.Add("currentPage", "1");
            this.BindInstruments();
        }
        if (Session["currengPage"].ToString() != "")
            this.BindInstruments();  //调用自定义方法绑定DataList控件,按条件显示商品列表
    }
    private void BindInstruments()
    {
        Instrument instru = new Instrument();
        DataTable dtHotGood = instru.GetHotest();
        
        //使用PagedDataSource类在DataList控件中实现分页
        PagedDataSource ps1 = new PagedDataSource();
        ps1.DataSource = dtHotGood.DefaultView;
        ps1.AllowPaging = true;
        ps1.PageSize = 6;
        ps1.CurrentPageIndex = Convert.ToInt32(Session["currentPage"]) - 1;
        this.dlHotGood.DataSource = ps1;
        this.dlHotGood.DataBind();    //与图中DataList控件dlHotGood绑定
        //获取总页数
        this.lbAllPage.Text = Convert.ToString(ps1.PageCount);
        //若当前页是第1页，则禁用“首页”“上一页”按钮
        if (Convert.ToInt32(Session["currentPage"]) == 1)
        {
            this.lbtnFormerPage.Enabled = false;
            this.lbtnFirstPage.Enabled = false;
        }
        else
        {
            this.lbtnFirstPage.Enabled = true;
            this.lbtnFormerPage.Enabled = true;
        }
        //若当前页是最后一页，则禁用“下一页”“尾页”按钮
        if (Convert.ToInt32(Session["currentPage"]) == ps1.PageCount)
        {
            this.lbtnNextPage.Enabled = false;
            this.lbtnLastPage.Enabled = false;
        }
        else
        {
            this.lbtnLastPage.Enabled = true;
            this.lbtnNextPage.Enabled = true;
        }
    }
    //单击“首页”
    protected void lbtnFirstPage_Click(object sender, EventArgs e)
    {
        Session["currentPage"] = "1";
        this.lbPage.Text = "1";
        this.BindInstruments();
    }
    //单击“上一页”
    protected void lbtnFormerPage_Click(object sender, EventArgs e)
    {
        Session["currentPage"] = Convert.ToInt32(Session["currentPage"]) - 1;
        this.lbPage.Text = Session["currentPage"].ToString();
        this.BindInstruments();
    }
    //单击“下一页”
    protected void lbtnNextPage_Click(object sender, EventArgs e)
    {
        Session["currentPage"] = Convert.ToInt32(Session["currentPage"]) + 1;
        this.lbPage.Text = Session["currentPage"].ToString();
        this.BindInstruments();
    }
    //单击“尾页”
    protected void lbtnLastPage_Click(object sender, EventArgs e)
    {
        Session["currentPage"] = this.lbAllPage.Text;
        this.lbPage.Text = this.lbAllPage.Text;
        this.BindInstruments();
    }
    protected void btnBuy_Click(object sender, EventArgs e)
    {
        if (Session["User"] != null) //用户若登录，则可购买商品
        {
            Button btnBuy = (Button)sender;
            int instruID = Convert.ToInt32(btnBuy.CommandArgument.ToString());    //从按钮CommandArgument属性值获得所购买商品的商品号。
            //判断Session对象中是否存在用于保存已购买商品的ArrayList对象orderList，若存在，则取出该对象，否则新建一个ArrayList对象orderList。
            if (Session["OrderList"] != null)
            {
                orderList = (ArrayList)Session["OrderList"];
            }
            else
            {
                orderList = new ArrayList();
            }
            DataTable dtGoodDetail = new DataTable();
            Instrument instru = new Instrument();
            ///////////////////////////
            ///////////////////////////
            //通过instruID得到商品的详细信息，存放到DataTable dtGoodDetail中
            ///////////////////////////
            ///////////////////////////
            dtGoodDetail = instru.GetSingleInstrument(instruID);
            if (dtGoodDetail.Rows.Count > 0)
            {
                Client user = (Client)Session["User"];  //从Session中获取User对象。
                OrderList newOrder = new OrderList(dtGoodDetail);   //用指定的商品编号。。。初始数理实例化Order类，得到新对象，用于表示新购买的一个商品。
                //判断新购买的商品是否购买过，如果已购买过，则将表示该商品的Order对象中的Number属性加1.
                for (int i = 0; i < orderList.Count; i++)
                {
                    OrderList order = (OrderList)orderList[i];
                    if (order.instruID == instruID)
                    {
                        order.quantity++;
                        Session["OrderList"] = orderList;
                        Response.Write("Cart.aspx");
                        return;
                    }
                }
                //若未购买过该商品，则将该商品的Order对象添加到ArrayList对象orderList中保存，并存入Session中。
                orderList.Add(newOrder);
                Session["OrderList"] = orderList;
                return;
            }
        }
        else
        {
            Response.Write(ccObj.MessageBox("请先登录！", "Default.aspx"));  //若未登录，则先登录
        }
    }
}
